package com.ytktt.study.week11.task9;

import com.ytktt.study.week11.task9.constant.RedisConstant;
import io.lettuce.core.RedisClient;
import io.lettuce.core.ScriptOutputType;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * pub 应用
 *
 * @author ytk
 */
public class PubApplication {

    public static void main(String[] args) throws InterruptedException {
        RedisClient redisClient = RedisClient.create(RedisConstant.REDIS_SERVER);

        ExecutorService executorService = Executors.newFixedThreadPool(6);
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 0; i < 6; i++) {
            int finalI = i;
            executorService.submit(() -> {
                StatefulRedisConnection<String, String> connection = redisClient.connect();
                RedisCommands<String, String> syncCommands = connection.sync();
                syncCommands.publish(RedisConstant.TOPIC, "java" + finalI);
                connection.close();
                countDownLatch.countDown();
            });
            Thread.sleep(200L);
        }
        countDownLatch.await();
        executorService.shutdown();

        redisClient.shutdown();
    }
}
